App = {};
App.debug = true;
App.MainApp = new Backbone.Marionette.Application();

App.MainApp.addInitializer(function() {
	if (App.debug) console.log('App.MainApp addInitializer');
	this.module('layoutModule').start({
		id: 'main-layout',
		template: 'main-layout-template',
		layout: {
			north: { size: 5,   minSize: 5,   maxSize: 5,  spacing_open: 0, spacing_closed: 0 },
			south: { size: 45,  minSize: 45,  maxSize: 45, spacing_open: 0, spacing_closed: 0 },
			west:  { size: 0,   minSize: 0,   maxSize: 0 },
			east:  { size: 250, minSize: 10,  maxSize: 250 }
		},
		region: this.mainRegion
	});
	this.module('sidebarlayoutModule').start({
		id: 'sidebar-layout',
		template: 'sidebar-layout-template',
		layout: {
			//north: { size: 0,   minSize: 0,   maxSize: 0, spacing_open: 0, spacing_closed: 0 },
			//south: { size: 0,   minSize: 0,   maxSize: 0, spacing_open: 0, spacing_closed: 0 },
			west:  { size: 0,   minSize: 0,   maxSize: 0, spacing_open: 0, spacing_closed: 0 },
			east:  { size: 8,   minSize: 8,   maxSize: 8, spacing_open: 0, spacing_closed: 0 }
		},
		region: this.sidebarRegion
	});
	this.module('containerModule').start({
		id: 'main-container',
		template: 'container-layout-template',
		region: App.MainApp.containerRegion,
		items: [
			{name: 'page1'},
			{name: 'page2'},
			{name: 'page3'}
		]
	});
	this.module('leafletModule').start({
		id: 'main-map-container',
		template: '#map-layout-template',
		lon: 51.505,
		lat: -0.09,
		zoom: 10,
		region: this.mapRegion
	});
	this.module('buttonbarModule').start({
		id: 'buttonbar-container',
		template: 'buttonbar-layout-template',
		region: this.buttonbarRegion
	});
	/*
	this.module('saveDialogModule').start();
	
	this.module('toolbarDialogModule').start({
		id: 'toolbarDialog',
		template: '#toolbar-dialog-layout-template'
	});
	*/
});

App.MainApp.on('initialize:before', function() {
	if (App.debug) console.log('App.MainApp initialize:before');
});
App.MainApp.on('initialize:after', function() {
	if (App.debug) console.log('App.MainApp initialize:after');
});
App.MainApp.on('start', function(options) {
	if (App.debug) console.log('App.MainApp start');
	$('#splash-screen').fadeOut('slow');
});

App.MainApp.vent.on("layoutModule:resized", function(data){
	if (App.debug) console.log("layout:resized event triggered.");
	App.MainApp.module('toolbarDialogModule').Controller.position();
	//App.MainApp.module('containerModule').Controller.resize(data.center);
	App.MainApp.module('leafletModule').Controller.redraw();
});

App.MainApp.vent.on("buttonbarModule:button_click", function(data){
	if (App.debug) console.log("buttonbarModule:button_click event triggered.");
	if (App.debug) console.log("buttonbarModule:button_click data id:" + data.id);
	App.MainApp.module('containerModule').Controller.change_tab(data.id);
});

App.MainApp.addRegions({
  mainRegion: "#main-region",
  mapRegion: "#page1",
  containerRegion: "#layout-center-region",
  sidebarRegion: '#main-layout-east',
  saveDialogRegion: '#save-dialog-region',
  toolbarDialogRegion: '#toolbar-dialog-region',
  buttonbarRegion: '#main-layout-south'
});